package com.jz.netthings.analysis.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.analysis.bean.operManage.NewProviderDisplay;
import com.jz.netthings.analysis.mapper.NewProviderDisplayMapper;
import com.jz.netthings.analysis.service.NewProviderDisplayService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.application.bodyCheck.bean.BodyCheck;
import com.jz.netthings.application.bodyCheck.mapper.mysql.BodyCheckMapper;
import com.jz.netthings.application.heatseal.bean.HeatSeal;
import com.jz.netthings.application.heatseal.mapper.mysql.HeatSealMapper;
import com.jz.netthings.application.providerCollection.bean.ProviderCollection;
import com.jz.netthings.application.providerCollection.mapper.mysql.ProviderCollectionMapper;
import com.jz.netthings.application.providerCost.bean.ProviderCost;
import com.jz.netthings.application.providerCost.mapper.mysql.ProviderCostMapper;
import com.jz.netthings.application.providerCreate.bean.ProviderXp;
import com.jz.netthings.application.providerCreate.mapper.mysql.ProviderXpMapper;
import com.jz.netthings.application.providerReg.bean.ProviderReg;
import com.jz.netthings.application.providerReg.mapper.mysql.ProviderRegMapper;
import com.jz.netthings.application.sample.bean.PlasmaSamples;
import com.jz.netthings.application.sample.mapper.PlasmaSamplesMapper;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 新浆员献浆过程时序图 服务实现类
 * </p>
 *
 * @author yangyunyun
 * @since 2019-07-22
 */
@Service
public class NewProviderDisplayServiceImpl extends ServiceImpl<NewProviderDisplayMapper, NewProviderDisplay> implements NewProviderDisplayService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ProviderInfoMapper providerInfoMapper;
    @Autowired
    ProviderRegMapper providerRegMapper;
    @Autowired
    BodyCheckMapper bodyCheckMapper;
    @Autowired
    PlasmaSamplesMapper plasmaSamplesMapper;
    @Autowired
    ProviderXpMapper providerXpMapper;
    @Autowired
    ProviderCollectionMapper providerCollectionMapper;
    @Autowired
    ProviderCostMapper providerCostMapper;
    @Autowired
    HeatSealMapper heatSealMapper;
    @Autowired
    NewProviderDisplayMapper newProviderDisplayMapper;


    @Override
    public void loadNewProviderDisplay(int year) throws AdscmSystemException {

        logger.info("构造["+year+"]年的新浆员献浆过程时序图");

        String beginTime = year + "-01-01 00:00:00";
        String endTime = year + "-12-31 23:59:59";

        Wrapper<ProviderInfo> infoWrapper = new EntityWrapper<>();
        infoWrapper.between("createDate",beginTime,endTime);
        infoWrapper.orderBy("createDate",true);
        List<ProviderInfo> infos = providerInfoMapper.selectList(infoWrapper);

        if (infos != null && infos.size()>0){
            for (ProviderInfo info : infos) {
                this.loadNewProviderDisplay(info);
            }
        }
    }


    @Override
    public NewProviderDisplay loadNewProviderDisplay(ProviderInfo info)throws  AdscmSystemException{
        if (info == null){
            throw new AdscmSystemException("浆员信息为null...");
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(info.getId(),null) && (info.getCreateDate() == null)){
            info = providerInfoMapper.selectById(info.getId());
        }

        logger.info("构造浆员["+info.getId()+"]建档那次献浆过程的时序图");
        Date createDate = info.getCreateDate();
        String cDate = AdscmDateUtils.getNowOnlyDateFomat1(createDate);
        String cDateTimes = AdscmDateUtils.getDateFomat(createDate);

        String beginTime = cDate + " 00:00:00";
        String endTime = cDate + " 23:59:59";

        boolean isInsert = false;
        NewProviderDisplay display = new NewProviderDisplay();
        display.setProviderId(info.getId());
        display.setCreateDate(cDate);
        display = newProviderDisplayMapper.selectOne(display);
        if (display == null){
            isInsert = true;
            display = new NewProviderDisplay();
            display.setProviderId(info.getId());
            display.setName(info.getName());
            display.setCreateDate(cDate);
        }
        display.setCreateTime(AdscmDateUtils.getIntegerTime(cDateTimes));

        Wrapper<ProviderReg> regWrapper = new EntityWrapper<>();
        regWrapper.eq("providerNo",display.getProviderId());
        regWrapper.between("regTime",beginTime,endTime);
        List<ProviderReg> regs = providerRegMapper.selectList(regWrapper);
        ProviderReg reg = null;
        if (regs != null && regs.size()>0){
            reg = regs.get(0);
        }else {
            logger.warn("没有找到浆员建档当天的登记记录，构造失败...");
            return display;
        }
        if (!AdscmBaseUtils.getInstance().isNotEmpty(reg.getRegisterNo(),null)){
            logger.warn("浆员建档当天的登记记录无登记号，构造失败...");
            return display;
        }


        //体检时间
        BodyCheck bodyCheck = new BodyCheck();
        bodyCheck.setProviderNo(display.getProviderId()+"");
        bodyCheck.setRegisterNo(reg.getRegisterNo());
        bodyCheck = bodyCheckMapper.selectOne(bodyCheck);
        if (bodyCheck != null){
            String bodyCheckTime = bodyCheck.getBodyEndTime();
            if (AdscmBaseUtils.getInstance().isNotEmpty(bodyCheckTime,null)){
                bodyCheckTime = AdscmDateUtils.getDateFomat(AdscmDateUtils.parseDateL(bodyCheckTime));
                display.setBodyCheckTime(AdscmDateUtils.getIntegerTime(bodyCheckTime));
            }
        }
        //小样采集时间
        PlasmaSamples plasmaSamples = new PlasmaSamples();
        plasmaSamples.setProviderNo(display.getProviderId()+"");
        plasmaSamples.setRegisterNo(reg.getRegisterNo());
        plasmaSamples = plasmaSamplesMapper.selectOne(plasmaSamples);
        if (plasmaSamples != null){
            Date gatheringTime = plasmaSamples.getGatheringTime();
            if (gatheringTime != null){
                display.setSampleAssayTime(AdscmDateUtils.getIntegerTime(AdscmDateUtils.getDateFomat(gatheringTime)));
            }
            //化检时间
            Date inspectionEndTime = plasmaSamples.getInspectionEndTime();
            if (inspectionEndTime != null){
                display.setAssayTime(AdscmDateUtils.getIntegerTime(AdscmDateUtils.getDateFomat(inspectionEndTime)));
            }
        }

        //胸片
        Wrapper<ProviderXp> xpWrapper = new EntityWrapper<>();
        xpWrapper.eq("providerNo",display.getProviderId());
        xpWrapper.between("createDate",beginTime,endTime);
        List<ProviderXp> xps = providerXpMapper.selectList(xpWrapper);
        if (xps != null && xps.size()>0){
            ProviderXp xp = xps.get(0);
            if (xp != null){
                Date xpCreateDate = xp.getCreateDate();
                if (xpCreateDate != null){
                    display.setChestCheckTime(AdscmDateUtils.getIntegerTime(AdscmDateUtils.getDateFomat(xpCreateDate)));
                }
            }
        }

        //采浆
        ProviderCollection coll = new ProviderCollection();
        coll.setProviderNo(display.getProviderId()+"");
        coll.setRegisterNo(reg.getRegisterNo());
        coll = providerCollectionMapper.selectOne(coll);
        if (coll != null){
            Date collEndTime = coll.getEndTime();
            if (collEndTime != null){
                display.setCollectionTime(AdscmDateUtils.getIntegerTime(AdscmDateUtils.getDateFomat(collEndTime)));
            }

        }

        //费用发放
        ProviderCost cost = new ProviderCost();
        cost.setProviderNo(display.getProviderId()+"");
        cost.setRegisterNo(reg.getRegisterNo());
        cost = providerCostMapper.selectOne(cost);
        if (cost != null){
            Date putEndTime = cost.getPutOutEndTime();
            if (putEndTime != null){
                display.setCostTime(AdscmDateUtils.getIntegerTime(AdscmDateUtils.getDateFomat(putEndTime)));
            }
        }

        //热合
        HeatSeal heatSeal = new HeatSeal();
        heatSeal.setProviderNo(display.getProviderId()+"");
        heatSeal.setRegisterNo(reg.getRegisterNo());
        heatSeal = heatSealMapper.selectOne(heatSeal);
        if (heatSeal != null){
            Date pTime = heatSeal.getPlasmaScanTime();
            if (pTime != null){
                display.setHeatTime(AdscmDateUtils.getIntegerTime(AdscmDateUtils.getDateFomat(pTime)));
            }
        }

        if (isInsert){
            newProviderDisplayMapper.insert(display);
        }else {
            newProviderDisplayMapper.updateById(display);
        }

        return display;
    }

}
